﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Enums.Schedule;
using VisionCloud.Core.Model.Rs;
using VisionCloud.Domain.Configs;
using VisionCloud.Domain.DtoModel.Rs;
using VisionCloud.Domain.DtoModel.Schedule.WorkOrder;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Rs;

namespace VisionCloud.Domain.Repository.Implement.Rs
{
    public class RsWorkOrderMapRepository : BaseRepository<RsWorkOrderMap>, IRsWorkOrderMapRepository
    {
        /// <summary>
        /// 根据Id取工单对象
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="companyGuid">公司Id</param>
        /// <returns></returns>
        public async Task<RsWorkOrderMap> GetEntity(WorkOrderRequestDto condition, Guid companyGuid)
        {
            var result = await (from p in Db.RsWorkOrderMap
                                where !p.IsDeleted
                                && p.CompanyGuid == companyGuid
                                && p.RsWorkOrderId == condition.WorkOrderId
                                select p).AsNoTracking().SingleOrDefaultAsync().ConfigureAwait(false);
            return result;
        }

        /// <summary>
        /// 根据项目Id获取列表
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="companyGuid">公司Id</param>
        /// <returns></returns>
        public async Task<List<RsWorkOrderMap>> GetEntitiesByProject(Guid projectId, Guid companyGuid)
        {
            var result = await (from p in Db.RsWorkOrderMap
                                where !p.IsDeleted
                                && p.CompanyGuid == companyGuid
                                && p.ProjectId == projectId
                                select p).AsNoTracking().ToListAsync().ConfigureAwait(false);
            return result;
        }

        /// <summary>
        /// 取工单信息
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="companyGuid">公司Id</param>
        /// <returns></returns>
        public async Task<List<WorkOrderMapResponseDto>> GetWorkOrderMapList(Guid projectId, Guid companyGuid)
        {
            var result = await (from p in Db.RsWorkOrderMap
                                join t in Db.RsWorkOrder on p.RsWorkOrderId equals t.Id
                                where !p.IsDeleted
                                && !t.IsDeleted
                                && p.CompanyGuid == companyGuid
                                && p.ProjectId == projectId
                                select new WorkOrderMapResponseDto()
                                {
                                    Id = p.Id,
                                    BpsTaskId = p.BpsTaskId,
                                    ProjectId = p.ProjectId.ToString(),
                                    ProjectName = t.ProjectName,
                                    ProcedureCode = t.ProcedureCode,
                                    ProcedureName = WorkOrderCodeAndNameMap.GetNameWithCode(t.ProcedureCode),
                                    BuildingName = t.BuildingName,
                                    FloorName = t.FloorName,
                                }).AsNoTracking().ToListAsync().ConfigureAwait(false);
            return result;
        }

        public async Task<List<GetWorkOrderListWithProjectInfoRspDto>> GetWorkOrderListWithProjectInfo(GetWorkOrderListWithProjectInfoRqDto request)
        {
            var workOrderMapQuery = Db.RsWorkOrderMap.Where(x => !x.IsDeleted);
            var workOrderQuery = Db.WorkOrders.Where(x => !x.IsDeleted && x.Status != WorkOrderStatusEnum.已完成)
                .Where(x => x.ProjectId == request.ProjectCode)
                .Where(x => x.BuildingCode == request.BuildingCode)
                .Where(x => x.FloorCode == request.FloorCode);
            var list = await workOrderMapQuery.Join(workOrderQuery, x => x.RsWorkOrderId, y => y.Id, (x, y) =>
            new GetWorkOrderListWithProjectInfoRspDto
            {
                ProcedureCode = y.ProcedureCode,
                BpsTaskId = y.BpsTaskId,
                WorkOrderId = y.Id
            }).ToListAsync();
            return list;
        }
    }
}
